﻿using Common.Device.Enums;
using Common.Device.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views.Devices
{
    /// <summary>
    /// ZhiKongView.xaml 的交互逻辑
    /// </summary>
    public partial class ZhiKongView : UserControl
    {

        //仪器对象
        public Interfaces.IZhiKong Device;
        ViewModels.ZhiKongViewModel viewModel = new ViewModels.ZhiKongViewModel();
        public ZhiKongView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            //InitializeComponent();
            //DataContext = viewModel;
            InitializeComponent();
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out ZhiKongEnum.TestMode mode);
                ZhiKong parameter = JsonConvert.DeserializeObject<ZhiKong>(order.deviceModel);
                switch (mode)
                {
                    case ZhiKongEnum.TestMode.开机确认:
                        viewModel.MenuIndex = 0;
                        viewModel.Channel1 = parameter.通道;
                        viewModel.Getcmd1 = parameter.getcmd;
                        viewModel.Getops1 = parameter.getops;
                        viewModel.Getdata1 = parameter.getdata;
                        break;
                    case ZhiKongEnum.TestMode.自动测试:
                        viewModel.MenuIndex = 1;
                        viewModel.Channel2 = parameter.通道;
                        viewModel.Setcmd2 = parameter.setcmd;
                        viewModel.Setops2 = parameter.setops;
                        viewModel.Setdata2 = parameter.setdata;

                        viewModel.Getcmd2 = parameter.getcmd;
                        viewModel.Getops2 = parameter.getops;
                        viewModel.Getdata2 = parameter.getdata;
                        break;
                    case ZhiKongEnum.TestMode.手动测试:
                        viewModel.MenuIndex = 2;
                        viewModel.Channel3 = parameter.通道;
                        viewModel.Setcmd3 = parameter.setcmd;
                        viewModel.Setops3 = parameter.setops;
                        viewModel.Setdata3 = parameter.setdata;

                        viewModel.Getcmd3 = parameter.getcmd;
                        viewModel.Getops3 = parameter.getops;
                        viewModel.Getdata3 = parameter.getdata;
                        break;
                    case ZhiKongEnum.TestMode.手动确认:
                        viewModel.MenuIndex = 3;
                        viewModel.Channel4 = parameter.通道;
                        viewModel.Setcmd4 = parameter.setcmd;
                        viewModel.Setops4 = parameter.setops;
                        viewModel.Setdata4 = parameter.setdata;

                        viewModel.Getcmd4 = parameter.getcmd;
                        viewModel.Getops4 = parameter.getops;
                        viewModel.Getdata4 = parameter.getdata;
                        break;
                    case ZhiKongEnum.TestMode.获取蓝牙:
                        viewModel.MenuIndex = 4;
                        viewModel.Channel5 = parameter.通道;
                        viewModel.Setcmd5 = parameter.setcmd;
                        viewModel.Setops5 = parameter.setops;
                        viewModel.Setdata5 = parameter.setdata;

                        viewModel.Getcmd5 = parameter.getcmd;
                        viewModel.Getops5 = parameter.getops;
                        viewModel.Getdata5 = parameter.getdata;
                        break;
                    case ZhiKongEnum.TestMode.获取CPU:
                        viewModel.MenuIndex = 5;
                        viewModel.Channel6 = parameter.通道;
                        viewModel.Setcmd6 = parameter.setcmd;
                        viewModel.Setops6 = parameter.setops;
                        viewModel.Setdata6 = parameter.setdata;

                        viewModel.Getcmd6 = parameter.getcmd;
                        viewModel.Getops6 = parameter.getops;
                        viewModel.Getdata6 = parameter.getdata;
                        break;
                    case ZhiKongEnum.TestMode.写入蓝牙:
                        viewModel.MenuIndex = 6;
                        viewModel.Setcmd7 = parameter.setcmd;
                        viewModel.Setops7 = parameter.setops;
                        viewModel.Setdata7 = parameter.setdata;

                        viewModel.Getcmd7 = parameter.getcmd;
                        viewModel.Getops7 = parameter.getops;
                        viewModel.Getdata7 = parameter.getdata;
                        viewModel.TestIndex = parameter.测试项序号;
                        viewModel.TestNum = parameter.函数编号;
                        break;
                    case ZhiKongEnum.TestMode.初次测试检查:
                        viewModel.MenuIndex = 7;
                        viewModel.TestIndex1 = parameter.测试项序号;
                        viewModel.TestNum1 = parameter.函数编号;
                        break;
                    case ZhiKongEnum.TestMode.复测检查:
                        viewModel.MenuIndex = 8;
                        viewModel.TestIndex2 = parameter.测试项序号;
                        viewModel.TestNum2 = parameter.函数编号;
                        break;
                    case ZhiKongEnum.TestMode.初次通过上传:
                        viewModel.MenuIndex = 9;
                        break;
                    case ZhiKongEnum.TestMode.复测上传:
                        viewModel.MenuIndex = 10;
                        break;
                    case ZhiKongEnum.TestMode.模式切换确认:
                        viewModel.MenuIndex = 11;
                        viewModel.Channel12 = parameter.通道;
                        viewModel.Getcmd12 = parameter.getcmd;
                        viewModel.Getops12 = parameter.getops;
                        viewModel.Getdata12 = parameter.getdata;
                        viewModel.Get12endtime = parameter.endtime;
                        break;

                    default:
                        break;
                }
            }
        }

        private void Click1(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.开机确认,
                通道 = viewModel.Channel1,
                getcmd = viewModel.Getcmd1,
                getops = viewModel.Getops1,
                getdata = viewModel.Getdata1,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click2(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.自动测试,
                通道 = viewModel.Channel2,
                setcmd = viewModel.Setcmd2,
                setops = viewModel.Setops2,
                setdata = viewModel.Setdata2,
                getcmd = viewModel.Getcmd2,
                getops = viewModel.Getops2,
                getdata = viewModel.Getdata2,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click3(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.手动测试,
                通道 = viewModel.Channel3,
                setcmd = viewModel.Setcmd3,
                setops = viewModel.Setops3,
                setdata = viewModel.Setdata3,
                
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click4(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.手动测试,
                通道 = viewModel.Channel4,
                setcmd = viewModel.Setcmd4,
                setops = viewModel.Setops4,
                setdata = viewModel.Setdata4,

            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click5(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.获取蓝牙,
                通道 = viewModel.Channel5,
                setcmd = viewModel.Setcmd5,
                setops = viewModel.Setops5,
                setdata = viewModel.Setdata5,

            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click6(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.获取CPU,
                通道 = viewModel.Channel6,
                setcmd = viewModel.Setcmd6,
                setops = viewModel.Setops6,
                setdata = viewModel.Setdata6,

            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click7(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.写入蓝牙,
                测试项序号 = viewModel.TestIndex,
                函数编号 = viewModel.TestNum,
                //通道 = viewModel.Channel6,
                setcmd = viewModel.Setcmd7,
                setops = viewModel.Setops7,
                setdata = viewModel.Setdata7,
                getcmd = viewModel.Getcmd7,
                getops = viewModel.Getops7,
                getdata = viewModel.Getdata7,

            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click8(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.初次测试检查,
                测试项序号 = viewModel.TestIndex1,
                函数编号 = viewModel.TestNum1,
                //通道 = viewModel.Channel6,
                

            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click9(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.复测检查,
                测试项序号 = viewModel.TestIndex2,
                函数编号 = viewModel.TestNum2,
                //通道 = viewModel.Channel6,


            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click10(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.初次通过上传,
                //测试项序号 = viewModel.TestIndex,
                //函数编号 = viewModel.TestNum,
                通道 = 1,


            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click11(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.复测上传,
                //测试项序号 = viewModel.TestIndex,
                //函数编号 = viewModel.TestNum,
                通道 = 1,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void Click12(object sender, RoutedEventArgs e)
        {
            Models.ZhiKong parameter = new Models.ZhiKong()
            {
                mode = ZhiKongEnum.TestMode.模式切换确认,
                //测试项序号 = viewModel.TestIndex,
                //函数编号 = viewModel.TestNum,
                通道 = viewModel.Channel12,
                //getcmd = viewModel.Getcmd12,
                //getops = viewModel.Getops12,
                //getdata = viewModel.Getdata12,
                endtime =  viewModel.Get12endtime,

            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }

        }
    }
}
